<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>Mail</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../lib/layui-v2.5.5/css/layui.css" media="all">
    <link rel="stylesheet" href="../css/public.css" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">
        <blockquote class="layui-elem-quote layui-text">
            使用邮箱功能前先要确定服务器25,465,587等端口可用.
        </blockquote>
        <fieldset class="layui-elem-field layui-field-title" style="margin-top: 50px;">
            <legend>邮箱设置</legend>
        </fieldset>
        <form class="layui-form layui-form-pane" action="" lay-filter="example">
            <input type="text" name="id" style="display: none;">
            <div class="layui-form-item">
                <label class="layui-form-label">smtp服务器</label>
                <div class="layui-input-block">
                    <input type="text" name="smtp" autocomplete="off" placeholder="请输入" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">发信邮箱</label>
                <div class="layui-input-block">
                    <input type="email" name="number" autocomplete="off" placeholder="请输入" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">发信密码</label>
                <div class="layui-input-block">
                    <input type="text" name="password" autocomplete="off" placeholder="请输入" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">邮箱端口</label>
                <div class="layui-input-block">
                    <input type="number" name="port" autocomplete="off" placeholder="请输入" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label class="layui-form-label">收信邮箱</label>
                <div class="layui-input-block">
                    <input type="email" name="letter" autocomplete="off" placeholder="请输入" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <button class="layui-btn layui-btn-normal" lay-submit="" lay-filter="mail_update">修改</button>
                <button class="layui-btn layui-btn-warm" lay-submit="" lay-filter="mail_test">发送测试</button>
                <button class="layui-btn layui-btn-danger" lay-submit="" lay-filter="mail_qk">清空</button>
            </div>
        </form>
    </div>
</div>
<script src="/lib/layui-v2.5.5/layui.js" charset="utf-8"></script>
<script>
    layui.use('form', function () {
        var $ = layui.jquery
            ,form = layui.form
            ,layer = layui.layer;
        $.ajax({url: "/selectmail",type: "GET",headers: {"token":localStorage.getItem("token")},async: false,cache: false,
            success: function (data) {
                form.val('example',data);
                form.render();//重新渲染页面
            },error: function (e) {layer.msg("请求错误！");}
        });
        //监听提交1
        form.on('submit(mail_update)', function (dataa) {
            var data = dataa.field
            $.ajax({url: "/updatemail",
                data: "id="+data.id+"&smtp="+data.smtp+"&port="+data.port+"&number="+data.number+"&password="+data.password+"&letter="+data.letter,
                type: "POST",headers: {"token":localStorage.getItem("token")},async: false,cache: false,
                success: function (datas) {
                    if(datas==1){
                        layer.msg("邮箱修改成功！")
                    }else {
                        layer.msg("邮箱修改失败！")
                    }
                },error: function (e) {layer.msg("请求错误！");}
            });
            return false;
        });
        //监听提交2
        form.on('submit(mail_test)', function (datas) {
            var data = datas.field;
            var test_i;
            $.ajax({url: '/mailtest',type: 'POST',async: true,cache: false,
                headers: {"token":localStorage.getItem("token")},
                data: "smtp="+data.smtp+"&port="+data.port+"&number="+data.number+"&password="+data.password+"&letter="+data.letter,
                beforeSend: function () {
                    test_i=showLoad();
                },
                success: function (res) {
                    if (res==1) {
                        closeLoad(test_i);
                        layer.msg("邮箱发送成功！")
                    }else{
                        closeLoad(test_i);
                        layer.msg("邮箱发送失败！");
                    }
                }
            })
            return false;
        });
        //监听提交3
        form.on('submit(mail_qk)', function (data) {
            form.val('example', {
                "smtp": "", "number": "", "port":"", "password": "", "letter": ""
            })
            return false;
        });
        function showLoad() {
            return layer.msg('玩命执行中...', {icon: 16,shade: [0.5, '#f5f5f5'],scrollbar: false,offset: 'auto', time:100000});
        }
        function closeLoad(index) {
            layer.close(index);
        }
        //自定义验证规则
        form.verify({
            title: function (value) {
                if (value.length < 5) {
                    return '标题至少得5个字符啊';
                }
            }
            , pass: [
                /^[\S]{6,12}$/
                , '密码必须6到12位，且不能出现空格'
            ]
            , content: function (value) {
                layedit.sync(editIndex);
            }
        });
    });
</script>

</body>
</html>